package com.mo.lib.utils

import android.app.Activity
import android.content.Context
import android.os.Build
import android.text.InputType
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.fragment.app.Fragment
import com.dylanc.longan.hideKeyboard
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method

/**
 * @ author：mo
 * @ data：2021/9/24:16:00
 * @ 功能：键盘相关
 */

/** 初始化键盘，设置点击外部隐藏软键盘*/
fun Fragment.initSoftKeyboard() = requireActivity().initSoftKeyboard()

fun Activity.initSoftKeyboard() {
    this.findViewById<View>(Window.ID_ANDROID_CONTENT).initSoftKeyboard()
}

fun View.initSoftKeyboard() {
    this.setClick {
        hideKeyboard()
    }
}

fun Context.getInputMethodManager() = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

/** 初始化键盘，设置点击外部隐藏软键盘*/
//fun Activity.initSoftKeyboard() {
//    this.findViewById<View>(Window.ID_ANDROID_CONTENT).setOnClickListener(View.OnClickListener { closeSoftKeyboard() })
//}

/** 隐藏软键盘*/
fun Activity.closeSoftKeyboard() {
    this.currentFocus?.clearFocus()
    getInputMethodManager().hideSoftInputFromWindow((this.currentFocus ?: this.window.decorView).windowToken, 0);
}

fun closeSoftKeyboard(view: EditText) {
    view.clearFocus()
    view.context.getInputMethodManager().hideSoftInputFromWindow(view.windowToken, 0);
}

fun isShowKeybord(view: EditText, isShow: Boolean) {
    if (isShow) {
        view.requestFocus()
        view.context.getInputMethodManager().showSoftInput(view, 0);
    } else {
        view.clearFocus()
        view.context.getInputMethodManager().hideSoftInputFromWindow(view.windowToken, 0);
    }
}

/** 开启软键盘  */
fun openSoftKeyboard(view: EditText) {
    view.requestFocus()
    view.context.getInputMethodManager().showSoftInput(view, 0);
}

/** 禁掉系统软键盘*/
fun Activity.hideSoftInputMethod(editText: EditText) {
    this.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
    val currentVersion = Build.VERSION.SDK_INT
    var methodName: String? = null
    if (currentVersion >= 16) { // 4.2
        methodName = "setShowSoftInputOnFocus"
    } else if (currentVersion >= 14) { // 4.0
        methodName = "setSoftInputShownOnFocus"
    }
    if (methodName == null) {
        editText.inputType = InputType.TYPE_NULL
    } else {
        val cls = EditText::class.java
        val setShowSoftInputOnFocus: Method
        try {
            setShowSoftInputOnFocus = cls.getMethod(methodName, Boolean::class.javaPrimitiveType)
            setShowSoftInputOnFocus.isAccessible = true
            setShowSoftInputOnFocus.invoke(editText, false)
        } catch (e: NoSuchMethodException) {
            editText.inputType = InputType.TYPE_NULL
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        } catch (e: IllegalArgumentException) {
            e.printStackTrace()
        } catch (e: InvocationTargetException) {
            e.printStackTrace()
        }
    }
}